package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.AnswerDao;
import com.zretc.support.dao.CollectionDao;
import com.zretc.support.dao.TopicmultDao;
import com.zretc.support.dao.TopicsingleDao;
import com.zretc.support.entity.Answer;
import com.zretc.support.entity.Collection;
import com.zretc.support.entity.Topicmult;
import com.zretc.support.entity.Topicsingle;
import com.zretc.support.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    CollectionDao collectionDao;
    @Autowired
    TopicsingleDao topicsingleDao;
    @Autowired
    TopicmultDao topicmultDao;
    @Autowired
    AnswerDao answerDao;

    @Override
    public int addCollection(Collection collection) {
        collectionDao.save(collection);
        return 0;
    }

    @Override
    public int cancelCollection(int collectionId) {
        collectionDao.deleteById(collectionId);
        return 0;
    }

    @Override
    public Page<Collection> getCollectionByPageWithUid(String userid, int page, int size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return collectionDao.getCollectionByPageWithUserId(userid,pageable);
    }

    @Override
    public List getByTopicInId(List<String> topicId) {
        //创建数组
        List list =new ArrayList();

        List<Topicsingle> list1=topicsingleDao.getByTopicInId(topicId);

        List<Topicmult> list2=topicmultDao.getByTopicInId(topicId);
        //添加
        list.add(list1);
        list.add(list2);

        return list;
    }

    @Override
    public Answer getTopicByTopicId(String topicId, String userId) {

        return answerDao.findAnswerByTopicIdAndUserId(topicId,userId);
    }

    @Override
    public Page<Collection> findCollByUserId(String userid, int page, int size) {
        Pageable pageable = PageRequest.of(page-1,size);

        return collectionDao.findCollByUserId(userid,pageable);
    }
}
